<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="servlets.message.*"%>
<%@ page import="org.hibernate.SessionFactory"%>
<%@ page import="org.hibernate.Session"%>
<%@ page import="java.util.List"%>
<%@ page import="hibernate.model.*"%>
<%@ page import="hibernate.util.HibernateUtil"%>
<%@ page import="hibernate.method.*"%>
<%@ page import="hibernate.method.UserMethods"%>
<script> 

//$(document).ready(function() {
 //   $('#inbox').dataTable();
//} );


$(".subjects").click(function(e){
	 e.preventDefault();
	 var clickedID = $(this).attr('id');
	  $.ajax({
	       url: "../MessageDetails",
	       type: "POST",//type of posting the data
	       data: {
		       messageID: clickedID
	       },
	       dataType: 'json',
	       success: function (json) {
			var data = json;
			$("#msg").hide().fadeIn(1000).html("<table class=\"table\">");
		    $("#msg").append("<tr><td>Nadawca:</td><td>"+data.sender+"</td></tr>");
		    $("#msg").append("<tr><td>Data:</td><td>"+data.date+"</td></tr>");
		    $("#msg").append("<tr><td>Godzina:</td><td>"+data.time+"</td></tr>");
		    $("#msg").append("<tr><td>Temat:</td><td>"+data.subject+"</td></tr>");
		    $("#msg").append("<tr><td>Tresc:</td><td>"+data.content+"</td></tr>");
		    $("#msg").append("</table>");
	       },
	       error: function(xhr, ajaxOptions, thrownError){
	       },
	       timeout : 15000
	  });

	});

</script>

	
<div class="page-header">
	<h2 class="page-title">Skrzynka odbiorcza</h2>
</div>

<br />

<div id="content">
<%
						SessionFactory sessionFactory = HibernateUtil
								.getSessionAnnotationFactory();
						Session s = sessionFactory.openSession();
						s.beginTransaction();
						MessageMethods msg_m = new MessageMethods();
						 Integer user_id = (Integer) session.getAttribute("user_id");
							response.setContentType("text/html");
							if (user_id != null) {
						List<Message> msglist = msg_m.list_by_receiver(sessionFactory, s, user_id);
						
					%> 
					  <div class="col-sm-3 col-lg-3" style="width:100%;">
       <!-- MAIL BLOCK -->
      		<!--  <div class="dash-unit">
      		<dtitle>Inbox (1)</dtitle>
      		<hr>-->
      		
			            
			         <%
		if (msglist.size() > 0) {
			%>
			<div class="framemail">
    			<div class="window">
			        <ul class="mail">
			        <%
	for (Message m1 : msglist) {
		int sender_id = m1.getSender_id();
		UserMethods u_m = new UserMethods();
		User sender = u_m.id_to_user(sessionFactory, s, sender_id);
		String sender_nick = sender.getUsername();
		String subject = m1.getSubject();
		String date = m1.getSend_date();
		String time = m1.getSend_time();
		
		if (m1.getReceive_date() == null){
		%>
		
		<li>
			                <i class="unread"></i>
			                <img class="avatar" src="main/userAvatar.jsp?user_id=<%=sender_id %>" alt="avatar">
			                <p class="sender"><% out.println(sender_nick); %></p>
			                <p class="message"><strong><% out.println("<a class=\"subjects\" id=\""+ m1.getMessage_id() + "\" href=\"#\">" + subject + "</a>"); %></strong> - tresc...</p>
			               
			            </li>
			            
			    <% }
		else {
		%>
   
			            <li>
			                <i class="read"></i>
			                <img class="avatar" src="main/userAvatar.jsp?user_id=<%=sender_id %>" alt="avatar">
			                <p class="sender"><% out.println(sender_nick); %></p>
			                <p class="message"><strong><% out.println("<a class=\"subjects\" id=\""+ m1.getMessage_id() + "\" href=\"#\">" + subject + "</a>"); %></strong> - tresc...</p>
			                
			            </li>
			            <%} %>

   
		<%
		}%>
		 </ul>
    			</div>
			</div>
		</div><!-- /dash-unit -->
    </div><!-- /span3 -->		
    <%
		}	else if (msglist.size() == 0 ) out.println("<p>Brak wiadomosci w skrzynce odbiorczej</p>");
		}
		s.getTransaction().commit();
		%>
		
		
		

    <link href="../social/main/assets/css/main.css" rel="stylesheet">
<br > <br> <br>
<br/><br />
<div id="messageDetails">
<div class="page-header">
<h2 class="page-title">Szczególy wiadomosci</h2>
</div>
<div id="msg">Kliknij na temat wiadomosci, której szczegoly chcesz zobaczyc</div>
</div>